#include "collector.h"

Collector::Collector(QObject *parent) : QObject(parent)
{
    receiver=nullptr;
    table=TD_MESSAGE;
}

bool Collector::connectDB()
{
    QSettings reg(REG_BOOTSTRAP,QSettings::NativeFormat);
    QString host=reg.value("ds_host").toString();
    if(host.isEmpty())
        host="127.0.0.1";

    int port=3306;
    port=reg.value("ds_port").toInt();
    if(port==0)
        port=3306;

    QString user=reg.value("ds_user").toString();
    if(user.isEmpty())
        return false;

    QString passwd=reg.value("ds_passwd").toString();
    if(passwd.isEmpty())
        return false;

    QString dbName=reg.value("ds_db").toString();
    if(dbName.isEmpty())
        return false;

    db=getConnection(host,port,user,passwd,dbName,SP_NAME);
    if(db.open())
        return true;
    else
    {
        appendLog(db.lastError().text(),QtServiceBase::Warning);
        return false;
    }
}

void Collector::dbTry()
{
    if(!db.isOpen())
        db.open();
}

void Collector::appendLog(const QString &event,QtServiceBase::MessageType type)
{
    QtServiceBase::instance()->logMessage(event,type);
}

void Collector::start()
{
    if(!connectDB())
    {
        appendLog("Unable to connect to database!!!!",QtServiceBase::Error);
        QCoreApplication::exit(DB_ERR);
        return;
    }

    startReceiver();
    appendLog(QString("%1 Started").arg(SP_NAME));
}

void Collector::stop()
{
    stopReceiver();
    appendLog(QString("%1 Stopped").arg(SP_NAME));
}

void Collector::startReceiver()
{
    if(receiver == nullptr)
    {
        receiver=new BaseUdpReceiver(nullptr,PORT_JOURNAL,false);
        connect(receiver,&BaseUdpReceiver::received,this,&Collector::receivedMessage);
        connect(&recvThread,&QThread::finished,receiver,&QObject::deleteLater);
        receiver->moveToThread(&recvThread);
    }
    recvThread.start();
}

void Collector::stopReceiver()
{
    recvThread.quit();
    recvThread.wait();
}

bool Collector::addMessage(const QString &source, const QString &type, const QString &level, const QString &content)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (date_,time_,source_,type_,level_,content_) values (?,?,?,?,?,?)")
                  .arg(table));
    QString date=QDate::currentDate().toString(DATE_FORMAT);
    QString time=QTime::currentTime().toString(TIME_FORMAT);
    query.addBindValue(date);
    query.addBindValue(time);
    query.addBindValue(source);
    query.addBindValue(type);
    query.addBindValue(level);
    query.addBindValue(content);

    if(query.exec())
        return true;
    else
    {
        appendLog(query.lastError().text(),QtServiceBase::Warning);
        return false;
    }
}

void Collector::receivedMessage(const QByteArray &data)
{
    if(data.isEmpty())
        return;

    QCborValue value=QCborValue::fromCbor(data);
    if(value.isMap())
    {
        QCborMap obj=value.toMap();
        QString source=obj.value("source").toString();
        QString type=obj.value("type").toString();
        QString level=obj.value("level").toString();
        QString content=obj.value("content").toString();
        addMessage(source,type,level,content);
    }
}
